/*
 * Created on 2006-02-06
 */
package dk.aiszone.lang.autosource;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import dk.aiszone.lang.autosource.formatter.JavaFormatterImpl;
import dk.aiszone.lang.autosource.generatorobjects.JavaFile;

/**
 * Convinience file for create a Java file.
 * 
 * @author <a href="mailto:ais@mail.dk">Ais</a>
 * @since 1.0
 * @version 1.0
 */
public class SourceFileGenerator {

    /**
     * Writes the specified <code>JavaFile</code> to the filesystem.
     * 
     * @param baseFolder
     *            The base folder in which to store the file
     * @param javaFile
     *            The <code>JavaFile</code> to store
     * @throws IOException
     *             Thrown if the operation fails.
     */
    public static void writeFile(String baseFolder, JavaFile javaFile) throws IOException {
        File file = new File(baseFolder + "/" + javaFile.getFilePath() + "/" + javaFile.getFileName());
        File folder = file.getParentFile();
        if (!folder.exists()) {
            folder.mkdirs();
        }
        if (!file.exists()) {
            if (!file.createNewFile()) {
                throw new IOException("Unable to create file: " + file.getAbsolutePath());
            }
        }

        System.out.println("-- Writing class file: " + file.getAbsolutePath());
        FileWriter out = new FileWriter(file);
        out.write(new JavaFormatterImpl().toJava(javaFile));
        out.close();
    }
}